package com.hzqy.service.wchat;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.hzqy.commons.utils.WChatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hzqy.commons.utils.CommonsMessage;
import com.hzqy.commons.utils.MessageUtil;
import com.hzqy.commons.utils.RedisUtils;
import com.hzqy.commons.utils.SystemConfigUtils;
import com.hzqy.persistence.dao.hqjc.HqjcDao;
import com.hzqy.persistence.dao.hqjc.MultirateDao;
import com.hzqy.persistence.dao.wchat.WchatTvUserWeixinDao;
import com.hzqy.web.login.SessionKey;
import com.hzqy.web.vo.CmContentVo;
import com.hzqy.web.vo.WchatTvUserVo;
import com.hzqy.web.vo.wchat.TextMessage;
import com.hzqy.web.vo.wx_tv.WsSessionVo;

@Service
public class WechatService {
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	private final static int CMDS_CATALOG_LIVE = 17; //此处为直播分类ID，特殊固定为17，后期可调整
	private final static int CMDS_CATALOG_KEY = 18; //此处为按键分类ID，特殊固定为18，后期可调整
	private final static int CMDS_CATALOG_URL = 20; //此处为第三方增值业务跳转分类ID，特殊固定为20，后期可调整
	private final static int CMDS_CATALOG_SEARCH = 21; //此处为搜索跳转分类ID，特殊固定为21，后期可调整

	@Autowired
	WchatTvUserWeixinDao wchatTvUserWeixinDao;
	@Autowired
	private HqjcDao hqjcDao;
	@Autowired
	private MultirateDao multirateDao;

	/**
	 * 处理微信发来的请求 未完成
	 * 
	 * @param request
	 * @return
	 */
	public String weixinPost(HttpServletRequest request) {
		String respMessage = null;
		try {
			Map<String, String> requestMap = MessageUtil.xmlToMap(request);// xml请求解析
			String fromUserName = requestMap.get("FromUserName");// 发送方帐号（open_id）
			String toUserName = requestMap.get("ToUserName");// 公众帐号
			String msgType = requestMap.get("MsgType");// 消息类型
			String content = requestMap.get("Content");// 消息内容

			LOG.debug("FromUserName is:" + fromUserName + ", ToUserName is:" + toUserName + ", MsgType is:" + msgType);
			
			if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_TEXT)) { // 文本消息
				// 这里根据关键字执行相应的逻辑，只有你想不到的，没有做不到的
				if (content.equals("xxx")) {}
				// 自动回复
				TextMessage text = new TextMessage();
				text.setContent("the text is" + content);
				text.setToUserName(toUserName);
				text.setFromUserName(fromUserName);
				text.setCreateTime(new Date().getTime() + "");
				text.setMsgType(msgType);
				respMessage = MessageUtil.textMessageToXml(text);
			} else if(msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_VOICE)) {
				String recvMessage = requestMap.get("Recognition");
				LOG.debug("收到语音消息:recvMessage="+recvMessage);
				WchatTvUserVo wchatTvUserVo = wchatTvUserWeixinDao.selectUserInfo(fromUserName);
				if(wchatTvUserVo!=null) {
					String serverId = wchatTvUserVo.getF_wtut_serverid();
					String message = null;
					try {
						message = parseWXMessage(serverId,recvMessage,fromUserName);
					} catch(Exception e) {
						LOG.error("解析命令字异常：",e);
					}
					if(message == null) {
						recvMessage = "您发送的语音无法识别机顶盒操作指令，语音为："+recvMessage;
					} else {
						LOG.debug("语音消息发向机顶盒命令字="+message);
						WsSessionVo stbSession = CommonsMessage.getStbSessionFromCache(serverId);
						if(stbSession == null || stbSession.getSession()==null) {
							recvMessage = "您微信绑定的机顶盒未正常登录，请检查！";
						} else {
							if(message.indexOf("|")!=-1) { //向机顶盒发起命令
								CommonsMessage.send(stbSession.getSession(), message);
								recvMessage = "您发送的语音为："+recvMessage;
							} else { //向微信回复
								recvMessage = message;
							}
						}
					}
					respMessage = getReturnWsMessage(fromUserName,toUserName,MessageUtil.RESP_MESSAGE_TYPE_TEXT,recvMessage);
				} else { //用户未绑定机顶盒
					recvMessage = "您的微信号未绑定您的机顶盒。";
					respMessage = getReturnWsMessage(fromUserName,toUserName,MessageUtil.RESP_MESSAGE_TYPE_TEXT,recvMessage);
				}
			} else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_EVENT)) {
				String eventType = requestMap.get("Event");// 事件类型
				if (eventType.equals(MessageUtil.EVENT_TYPE_SUBSCRIBE)) {// 订阅
					String eventKey = requestMap.get("EventKey");//带参数二维码参数
					if(eventKey.indexOf("serverID:") != -1){
						respMessage = parseWXQRCode(fromUserName,toUserName,eventKey);
					}
				} else if (eventType.equals(MessageUtil.EVENT_TYPE_UNSUBSCRIBE)) {// 取消订阅
					// TODO 取消订阅后用户再收不到公众号发送的消息，因此不需要回复消息
				} else if (eventType.equals(MessageUtil.EVENT_TYPE_CLICK)) {// 自定义菜单点击事件
					String eventKey = requestMap.get("EventKey");// 事件KEY值，与创建自定义菜单时指定的KEY值对应
					LOG.error("event未知自定义菜单KEY值,eventKey="+eventKey);
				} else if (eventType.equals(MessageUtil.EVENT_TYPE_SCAN)) {// 扫码推送事件
					String eventKey = requestMap.get("EventKey");//带参数二维码参数
					if(eventKey.indexOf("serverID:") != -1){
						respMessage = parseWXQRCode(fromUserName,toUserName,eventKey);
					}
				} else {
					LOG.error("event未知事件类型,eventType="+eventType);
				}
			} else {
				LOG.error("msgType未知消息类型,msgType="+msgType);
			}
		} catch (Exception e) {
			LOG.error("error......", e);
		}
		return respMessage;
	}

	private String parseWXQRCode(String fromUserName, String toUserName, String eventKey){
		eventKey = eventKey.substring(eventKey.indexOf("serverID:")+9,eventKey.length());
		String sid="";//用户id
		String acid="";//活动id
		String prizeid="";//奖品id
		String prizename="";//奖品
		String info[],acids[];//根绝传递信息格式不同分割：a_b-c 或者b_c
		if(eventKey.indexOf("_")!=-1){
			String[] arr=eventKey.split("\\_");
			sid=arr.length>=1?arr[0]:"";
			acid=arr.length>=2?arr[1]:"";
			prizeid=arr.length>=3?arr[2]:"";
			if(eventKey.indexOf("-")!=-1){
				info=eventKey.split("-");
				prizename=info[1];
				if(info[0].indexOf("_")!=-1){//a_b-c
					acids=info[0].split("_");
					acid=acids[1];
					prizeid=acids.length>=3?acids[2]:"";
				}else{//b-c
					acid=info[0];
				}
			}
		}else{
			sid=eventKey;
		}
		String urlDomain = WChatUtils.getDomain();
		long time = System.currentTimeMillis()/1000;
		String resultUrl = urlDomain+"gd_service/activity/20200120gd_qrcode/mobile/index.jsp?serverId="+sid+"&activityId="+acid+"&prizeId="+prizename+"&times="+time;
		String picUrl =  urlDomain+"gd_service/activity/20200120gd_qrcode/mobile/imgs/dot.gif";
		String nameStr = "TV豆换好礼！\n账号:"+sid;
		String descripStr ="您正在兑换"+prizename+"，请填写详细的收货地址和手机号码";
		String respMessage="";
		List<Map<String,Object>> messageList=new ArrayList<Map<String,Object>>();
		Map<String,Object> msgMap=new HashMap<String,Object>();
		String activityInfo=null;
		try{
			activityInfo=RedisUtils.getString("activity_userinfo", sid+"_"+acid);
		}catch(Exception e){
			LOG.error("redis数据获取异常",e);
		}
		if(activityInfo!=null){
			String activityPath="activity_"+acid;
			nameStr="收货地址填写\n账号:"+sid;
			String pic="/mobile/imgs/bg.jpg";
			descripStr = "已获得"+prizename+"，请填写收货信息";
			if(activityInfo!=null&&activityInfo.indexOf("_")>0){
	    		String[] infos=activityInfo.split("_");
	    		activityPath=infos[0];
	    		nameStr=infos[1]+"\n账号:"+sid;
	    		if(infos.length>=3){
	    			pic=infos[2];
	    		}
	    		if(infos.length>=4){
	    			descripStr=infos[3];
	    		}
	    	}
			picUrl =  urlDomain+"gd_service/activity/"+activityPath+pic;
			resultUrl = urlDomain+"gd_service/activity/"+activityPath+"/mobile/index.jsp?serverId="+sid+"&activityId="+acid+"&prizeName="+prizename+"&prizeId="+prizeid+"&times="+time;
			
		}else if ("112".equals(acid)){
			LOG.error("redis方法未执行——需要进行跟进活动112-20200910septemberMovie");
			picUrl =  urlDomain+"gd_service/activity/20200910septemberMovie/mobile/imgs/bg.jpg";
			resultUrl = urlDomain+"gd_service/activity/20200910septemberMovie/mobile/index.jsp?serverId="+sid+"&activityId="+acid+"&prizeName="+prizename+"&prizeId="+prizeid+"&times="+time;
			nameStr = "恭喜您获得"+prizename+"！";
			descripStr = "您已抽中"+prizename+"，请填写详细的收货地址和手机号码";
		}else if ("108".equals(acid)){
			picUrl =  urlDomain+"gd_service/activity/20200812escape_room/mobile/imgs/bg.jpg";
			resultUrl = urlDomain+"gd_service/activity/20200812escape_room/mobile/index.jsp?serverId="+sid+"&activityId="+acid+"&prizeName="+prizename+"&prizeId="+prizeid+"&times="+time;
			nameStr = "恭喜您获得"+prizename+"！";
			descripStr = "您已抽中"+prizename+"，请填写详细的收货地址和手机号码";
		}else if ("20200807".equals(acid)){
			picUrl =  urlDomain+"gd_service/activity/20200807travel_fund/mobile/imgs/bg.jpg";
			resultUrl = urlDomain+"gd_service/activity/20200807travel_fund/mobile/index.jsp?serverId="+sid+"&activityId="+acid+"&prizeName="+prizename+"&prizeId="+prizeid+"&times="+time;
			nameStr = "恭喜您获得"+prizename+"！";
			descripStr = "您已抽中"+prizename+"，请填写详细的收货地址和手机号码";
		}else if ("106".equals(acid)){
			picUrl =  urlDomain+"gd_service/activity/20200815qixi_movie/mobile/imgs/bg.jpg";
			resultUrl = urlDomain+"gd_service/activity/20200815qixi_movie/mobile/index.jsp?serverId="+sid+"&activityId="+acid+"&prizeName="+prizename+"&prizeId="+prizeid+"&times="+time;
			nameStr = "恭喜您获得"+prizename+"！";
			descripStr = "您已抽中"+prizename+"，请填写详细的收货地址和手机号码";
		}else if ("105".equals(acid)){
			picUrl =  urlDomain+"gd_service/activity/20200814baby_tool/mobile/imgs/bg.jpg";
			resultUrl = urlDomain+"gd_service/activity/20200814baby_tool/mobile/index.jsp?serverId="+sid+"&activityId="+acid+"&prizeName="+prizename+"&prizeId="+prizeid+"&times="+time;
			nameStr = "恭喜您获得"+prizename+"！";
			descripStr = "您已抽中"+prizename+"，请填写详细的收货地址和手机号码";
		}else if ("20200717".equals(acid)){
			picUrl =  urlDomain+"gd_service/activity/20200717iptv_prize_survey/mobile/imgs/bg.jpg";
			resultUrl = urlDomain+"gd_service/activity/20200717iptv_prize_survey/mobile/index.jsp?serverId="+sid+"&activityId="+acid+"&prizeName="+prizename+"&prizeId="+prizeid+"&times="+time;
			nameStr = "恭喜您获得"+prizename+"！";
			descripStr = "奖品将以卡密形式发送短信给您，请准确填写手机号码。";
		}else if ("104".equals(acid)){
			picUrl =  urlDomain+"gd_service/activity/20200722super_kids/mobile/imgs/bg.jpg";
			resultUrl = urlDomain+"gd_service/activity/20200722super_kids/mobile/index.jsp?serverId="+sid+"&activityId="+acid+"&prizeName="+prizename+"&prizeId="+prizeid+"&times="+time;
			nameStr = "恭喜你获得奖品！";
			descripStr = "您已抽中"+prizename+"，请填写详细的收货地址和手机号码";
		}else if ("101".equals(acid)){
			picUrl =  urlDomain+"gd_service/activity/20200717sishi/mobile/imgs/bg.png";
			resultUrl = urlDomain+"gd_service/activity/20200717sishi/mobile/index.jsp?serverId="+sid+"&activityId="+acid+"&prizeName="+prizename+"&prizeId="+prizeid+"&times="+time;
			nameStr = "恭喜你获得奖品！";
			descripStr = "您已抽中"+prizename+"，请填写详细的收货地址和手机号码";
		}else if ("99".equals(acid)){
			picUrl =  urlDomain+"gd_service/activity/20200703sausage_island_marksman/mobile/imgs/bg.jpg";
			resultUrl = urlDomain+"gd_service/activity/20200703sausage_island_marksman/mobile/index.jsp?serverId="+sid+"&activityId="+acid+"&prizeName="+prizename+"&prizeId="+prizeid+"&times="+time;
			nameStr = "谁是香肠岛最佳神枪手活动奖品领取！";
			descripStr = "您已抽中"+prizename+"，请填写详细的收货地址和手机号码";
		}else if ("100".equals(acid)){
			picUrl =  urlDomain+"gd_service/activity/20200628childrenOrderYh/mobile/imgs/bg.jpg";
			resultUrl = urlDomain+"gd_service/activity/20200628childrenOrderYh/mobile/index.jsp?serverId="+sid+"&activityId="+acid+"&prizeName="+prizename+"&prizeId="+prizeid+"&times="+time;
			nameStr = "恭喜你获得奖品！";
			descripStr = "您已抽中"+prizename+"，请填写详细的收货地址和手机号码";
		}else if ("20200630".equals(acid)){
			picUrl =  urlDomain+"gd_service/activity/20200630undersea_adventure/mobile/imgs/bg.jpg";
			resultUrl = urlDomain+"gd_service/activity/20200630undersea_adventure/mobile/index.jsp?serverId="+sid+"&activityId="+acid+"&prizeName="+prizename+"&prizeId="+prizeid+"&times="+time;
			nameStr = "海底大冒险活动奖品领取！";
			descripStr = "您已抽中"+prizename+"，请填写详细的收货地址和手机号码";
		}else if ("20200618".equals(acid)){
			picUrl =  urlDomain+"gd_service/activity/20200618experience/mobile/imgs/bg.jpg";
			resultUrl = urlDomain+"gd_service/activity/20200618experience/mobile/index.jsp?serverId="+sid+"&activityId="+acid+"&prizeName="+prizename+"&prizeId="+prizeid+"&times="+time;
			nameStr = "IPTV“新版体验官”招募！";
			descripStr = "欢迎加入新版“千人体验团”努力成为“新版体验官”吧！";
		}else if ("98".equals(acid)){
			picUrl =  urlDomain+"gd_service/activity/20200612movie_lott/mobile/imgs/bg.png";
			resultUrl = urlDomain+"gd_service/activity/20200612movie_lott/mobile/index.jsp?serverId="+sid+"&activityId="+acid+"&prizeName="+prizename+"&prizeId="+prizeid+"&times="+time;
			nameStr = "观影有礼活动奖品领取！";
			descripStr = "恭喜您抽中"+prizename+"，请填写领奖信息";
		}else if ("96".equals(acid)){
			picUrl =  urlDomain+"gd_service/activity/20200609clip_doll/mobile/imgs/bg.jpg";
			resultUrl = urlDomain+"gd_service/activity/20200609clip_doll/mobile/index.jsp?serverId="+sid+"&activityId="+acid+"&prizeName="+prizename+"&prizeId="+prizeid+"&times="+time;
			nameStr = "欢乐夹娃娃活动奖品领取！";
			descripStr = "您已抽中"+prizename+"，请填写详细的收货地址和手机号码";
		}else if ("94".equals(acid)||"95".equals(acid)){
			picUrl =  urlDomain+"gd_service/activity/20200604TrafficLive/mobile/imgs/bg.jpg";
			resultUrl = urlDomain+"gd_service/activity/20200604TrafficLive/mobile/index.jsp?serverId="+sid+"&activityId="+acid+"&prizeName="+prizename+"&prizeId="+prizeid+"&times="+time;
			nameStr = "交警直播活动奖品领取！";
			descripStr = "恭喜您抽中"+prizename+"，请填写领奖信息";
		}else if ("92".equals(acid)){
			picUrl =  urlDomain+"gd_service/activity/20200518aurora_ac/mobile/imgs/bg.jpg";
			resultUrl = urlDomain+"gd_service/activity/20200518aurora_ac/mobile/index.jsp?serverId="+sid+"&activityId="+acid+"&prizeName="+prizename+"&prizeId="+prizeid+"&times="+time;
			nameStr = "极光TV火爆上线，这“夏”有礼啦！";
			descripStr = "恭喜您抽中"+prizename+"，请填写领奖信息";
		}else if("86".equals(acid) || "87".equals(acid) || "88".equals(acid) || "89".equals(acid) || "90".equals(acid) || "91".equals(acid)){
			picUrl =  urlDomain+"gd_service/activity/20200508CartoonBox/mobile/imgs/bg.jpg";
			resultUrl = urlDomain+"gd_service/activity/20200508CartoonBox/mobile/index.jsp?serverId="+sid+"&activityId="+acid+"&prizeName="+prizename+"&prizeId="+prizeid+"&times="+time;
			nameStr = "趣味盲盒欢乐\"送\"活动奖品领取！";
			descripStr = "恭喜你抢到"+prizename+"，点击填写联系方式领奖吧";
		}else if("84".equals(acid) || "85".equals(acid)){
			picUrl =  urlDomain+"gd_service/activity/20200422mother/mobile/imgs/ac.jpg";
			resultUrl = urlDomain+"gd_service/activity/20200422mother/mobile/index.jsp?serverId="+sid+"&activityId="+acid+"&prizeName="+prizename+"&prizeId="+prizeid+"&times="+time;
			nameStr = "送给妈妈爱的礼物活动奖品领取！";
			descripStr = "恭喜你抢到"+prizename+"，点击填写联系方式领奖吧";
		}else if("83".equals(acid)){
			picUrl =  urlDomain+"gd_service/activity/20200410safeEntry/mobile/imgs/ac.jpg";
			resultUrl = urlDomain+"gd_service/activity/20200410safeEntry/mobile/index.jsp?serverId="+sid+"&activityId="+acid+"&prizeName="+prizename+"&prizeId="+prizeid+"&times="+time;
			nameStr = "安全闯关大冒险活动奖品领取！";
			descripStr = "您已抽中"+prizename+"，请填写详细的收货地址和手机号码";
		}else if("20200318".equals(acid)){
			picUrl =  urlDomain+"gd_service/activity/20200316opera/mobile/imgs/logo.jpg";
			resultUrl = urlDomain+"gd_service/activity/20200316opera/mobile/index.jsp?serverId="+sid+"&activityId=20200120&prizeId="+prizename+"&times="+time;
			nameStr = "梨园争榜首活动报名！";
			descripStr = "您正在参加戏曲知识竞答，月度/季度前三名可获超值好礼。";
		}else if("81".equals(acid)){
			resultUrl = urlDomain+"gd_service/activity/20200120gd_qrcode/mobile/index.jsp?serverId="+sid+"&activityId=20200120&prizeId="+prizename+"&times="+time;
			nameStr = "3.8女王节活动奖品领取！";
			descripStr = "您已抽中"+prizename+"，请填写详细的收货地址和手机号码";
		}else if("82".equals(acid)){
			nameStr = "开学冲刺跑密林大寻宝活动奖品领取！";
			descripStr = "您已抽中"+prizename+"，请填写详细的收货地址和手机号码";
			resultUrl = urlDomain+"gd_service/activity/20200316explore_ac/mobile/index.jsp?serverId="+sid+"&activityId=20200120&prizeId="+prizename+"&times="+time;
		}else if("20200812001".equals(acid)){
			nameStr = "开学了！";
			descripStr = "您已抽中"+prizename+"，请填写详细的收货地址和手机号码";
			picUrl =  urlDomain+"gd_service/activity/20200812lxkxj/mobile/imgs/logo.jpg";
			resultUrl = urlDomain+"gd_service/activity/20200812lxkxj//mobile/index.jsp?serverId="+sid+"&activityId=20200812001&prizeId="+prizeid+"&prizeName="+prizename+"&times="+time;
		}else if("111".equals(acid)){
			LOG.error("redis方法未执行——需要进行跟进活动111");
			nameStr = "感恩好礼！";
			descripStr = "您已领取"+prizename+"，请填写详细的收货地址和手机号码";
			picUrl =  urlDomain+"gd_service/activity/20200904IncreaseVip/mobile/imgs/logo.jpg";
			resultUrl = urlDomain+"gd_service/activity/20200904IncreaseVip//mobile/index.jsp?serverId="+sid+"&activityId="+acid+"&prizeId="+prizeid+"&prizeName="+prizename+"&times="+time;
		} else if("109".equals(acid) || "110".equals(acid)){
			picUrl =  urlDomain+"gd_service/activity/20200827park_tickets/mobile/imgs/bg.jpg";
			resultUrl = urlDomain+"gd_service/activity/20200827park_tickets/mobile/index.jsp?serverId="+sid+"&activityId="+acid+"&prizeName="+prizename+"&prizeId="+prizeid+"&times="+time;
			nameStr = "福利到！乐园门票活动奖品领取！";
			descripStr = "您已获得"+prizename+"，请填写详细的收货地址和手机号码";
		}
		//绝对不能加else，不然会覆盖TV豆活动的微信扫码
		msgMap.put("resultUrl", resultUrl); 
		msgMap.put("picUrl", picUrl);
		msgMap.put("nameStr", nameStr);
		msgMap.put("descripStr", descripStr);
		messageList.add(msgMap);
		respMessage = getReturnArticlesMessage(fromUserName,toUserName,messageList);
		
		/*String pushPic = urlDomain+"gd_service/activity/20190318ddzds/imgs/mobile/wxArticles1.png";//推送pic
		String pushUrl = urlDomain+"gd_service/activity/20190318ddzds/mobile/wxintroduce.jsp";//推送url
		String[] pics = {picUrl,pushPic};//报名 ，推送
		String[] urls = {resultUrl,pushUrl};//报名地址，推送地址
		String[] names = {"报名","报名须知"};*/
		
		return respMessage;
	}
	
	private String parseWXMessage(String serverId, String msg, String fromUserName) {
		String returnMsg = null;
		String name = "";
		String code = null;
		HashMap<String, Integer> map = new HashMap<String, Integer>();
		map.put("key", CMDS_CATALOG_KEY);
		map.put("url", CMDS_CATALOG_URL);
		map.put("liv", CMDS_CATALOG_LIVE);
		Iterator<String> iter = map.keySet().iterator();
		while (iter.hasNext()) {
			String key = iter.next();
			int val = map.get(key);
			CmContentVo contentVo = new CmContentVo();
			List<CmContentVo> contentList = hqjcDao.selectContentListByCatalogId(val, 0+"", 0, 100000,"2",contentVo);
			if(contentList == null)
				continue;
			for(CmContentVo content:contentList) {
				name = content.getF_cc_name();
				if(msg.indexOf(name)!=-1) {
					code = content.getF_cc_code();
					break;
				}
			}
			if(code!=null && !code.trim().equals("")) {
				returnMsg = key+"|"+serverId+"|"+code.trim();
				break;
			}
		}/*
		if(returnMsg==null || returnMsg.trim().length()<7) {
			List<CmContentVo> searchList = hqjcDao.selectContentListByCatalogId(CMDS_CATALOG_SEARCH, 0, 0, 1);
			LOG.debug("无法匹配命令字，最终指向搜索地址数量："+searchList.size());
			LOG.debug("无法匹配命令字，最终指向搜索地址："+searchList.get(0).getF_cc_code());
			returnMsg = "url|"+serverId+"|"+searchList.get(0).getF_cc_code();
		}*/
		if(returnMsg==null || returnMsg.trim().length()<7) {
			returnMsg = null;
			if(msg.startsWith("我要看")) {
				msg = msg.substring(3, msg.length()-1);
				List<CmContentVo> contentList = multirateDao.selectContentList(0, 0, 10, "2", 10,"", msg);
				if(msg.length()>=10)
					msg = msg.substring(0,5)+"...";
				if(contentList == null || contentList.size()==0) {
					returnMsg = "您要观看的电影名称为："+msg+"，平台未找到对应片源，请试试其他的关键字。";
				} else {
					returnMsg = "您要观看的电影名称为："+msg+"，平台为您找到了以下片源：\n";
					String url = SystemConfigUtils.getSysConfig(SessionKey.SESSION_SYSTEM_CONFIG.public_net_url.getKey());
					for(CmContentVo cv:contentList) {
						returnMsg += cv.getF_cc_name()+"   <a href=\""+url+"mobile_tv_wchat_service/mobile_to_tv.jsp";
						returnMsg += "?id="+cv.getF_cc_id()+"&category=3&openid="+fromUserName+"&serverid="+serverId+"\">投屏</a>\n";
//					returnMsg += "孤独的美食家第二季   <a href=\"http://zjwx.i-dong.com/gd_hqjc_web/mobile_tv_wchat_service/mobile_to_tv.jsp?id=483&category=3&openid="+fromUserName+"&serverid="+serverId+"\">投屏</a>\n";
//					returnMsg += "孤独的美食家第三季   <a href=\"http://zjwx.i-dong.com/gd_hqjc_web/mobile_tv_wchat_service/mobile_to_tv.jsp?id=13&category=3&openid="+fromUserName+"&serverid="+serverId+"\">投屏</a>\n";
					}
				}
			}
		}
		return returnMsg;
	}
	
	private String getReturnWsMessage(String fromUserName,String toUserName,String msgType,String recvMessage) {
		long createTime = System.currentTimeMillis()/1000;
		String respMessage = "<xml>";
		respMessage += "<ToUserName><![CDATA["+fromUserName+"]]></ToUserName>";
		respMessage += "<FromUserName><![CDATA["+toUserName+"]]></FromUserName>";
		respMessage += "<CreateTime>"+createTime+"</CreateTime>";
		respMessage += "<MsgType><![CDATA["+MessageUtil.RESP_MESSAGE_TYPE_TEXT+"]]></MsgType>";
		respMessage += "<Content><![CDATA["+recvMessage+"]]></Content>";
		respMessage += "</xml>";
		return respMessage;
	}

	private String getReturnArticlesMessage(String fromUserName,String toUserName, String[] picurl,String[] url ,String[] name,String[] description) {
		String respMessage = "<xml>";
		respMessage += "<ToUserName><![CDATA["+fromUserName+"]]></ToUserName>";
		respMessage += "<FromUserName><![CDATA["+toUserName+"]]></FromUserName>";
		respMessage += "<CreateTime>"+System.currentTimeMillis()/1000+"</CreateTime>";
		respMessage += "<MsgType><![CDATA[news]]></MsgType>";
		
		respMessage += "<ArticleCount>"+picurl.length+"</ArticleCount>";
		respMessage += "<Articles>";
		for(int i=0;i<picurl.length;i++) {
			respMessage += "<item>";
			respMessage += "<Title><![CDATA["+name[i]+"]]></Title>";
			respMessage += "<Description><![CDATA["+description[i]+"]]></Description>";
			respMessage += "<PicUrl><![CDATA["+picurl[i]+"]]></PicUrl>";
			respMessage += "<Url><![CDATA["+url[i]+"]]></Url>";
			respMessage += "</item>";
		}
		/*respMessage += "<item>";
		respMessage += "<Title><![CDATA[报名页面1]]></Title>";
		respMessage += "<Description><![CDATA[开始报名吧1]]></Description>";
		respMessage += "<PicUrl><![CDATA["+picurl+"]]></PicUrl>";
		respMessage += "<Url><![CDATA["+url+"]]></Url>";
		respMessage += "</item>";*/
		
		respMessage += "</Articles>";
		respMessage += "</xml>";
		return respMessage;
	}
	private String getReturnArticlesMessage(String fromUserName,String toUserName, List<Map<String,Object>> mesList) {
		String respMessage = "<xml>";
		respMessage += "<ToUserName><![CDATA["+fromUserName+"]]></ToUserName>";
		respMessage += "<FromUserName><![CDATA["+toUserName+"]]></FromUserName>";
		respMessage += "<CreateTime>"+System.currentTimeMillis()/1000+"</CreateTime>";
		respMessage += "<MsgType><![CDATA[news]]></MsgType>";
		
		respMessage += "<ArticleCount>"+mesList.size()+"</ArticleCount>";
		respMessage += "<Articles>";
		for(int i=0;i<mesList.size();i++) {
			respMessage += "<item>";
			respMessage += "<Title><![CDATA["+mesList.get(i).get("nameStr")+"]]></Title>";
			respMessage += "<Description><![CDATA["+mesList.get(i).get("descripStr")+"]]></Description>";
			respMessage += "<PicUrl><![CDATA["+mesList.get(i).get("picUrl")+"]]></PicUrl>";
			respMessage += "<Url><![CDATA["+mesList.get(i).get("resultUrl")+"]]></Url>";
			respMessage += "</item>";
		}
		respMessage += "</Articles>";
		respMessage += "</xml>";
		return respMessage;
	}
}